﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.Modules;

namespace ECRMS.WinForm.BaseClass.Common
{
    //员工
    public class Employee
    {
        private List<CtEmployee> _Employees;
        public CtEmployee OperatorRow;
        public List<int> OperatorRowCtFunctions;

        public List<CtEmployee> Employees
        { 
            get{
                return _Employees;
            }
            set{
                _Employees=value;
                _ResetFunctionNote(_Employees);
            }
        }

        private void _ResetFunctionNote(List<CtEmployee> _Employees)
        {
            foreach (var employee in _Employees)
            {
                string functionNotes="";
                List<int> list = employee.Functions.ToList();
                list.Sort();
                for (int i = 0; i < list.Count(); i++)
                {
                    if (i == 0)
                    {
                        functionNotes = functionNotes + ModuleEnum.GetName(typeof(ModuleEnum), employee.Functions[i])+"\r";
                    }
                    else
                    {
                        functionNotes = functionNotes + ",\r " + ModuleEnum.GetName(typeof(ModuleEnum), employee.Functions[i]);
                    }
                }
                    employee.FunctionNotes=functionNotes;
            }
        }


        public FuncResult GetEmployees(Guid corpId)
        {
            using (var ser = new ECRServiceClient())
            {
                CtServiceResultOfArrayOfCtEmployeeOUJwPUXg result = ser.CtGetEmployees(corpId);

                if (result.Status == 1)
                {
                    this.Employees = result.Data;
                }
                return new FuncResult<CtEmployee>
                {
                    Message = result.Message,
                    Status = result.Status
                };
            }
        }

        public FuncResult InsertEmployee()
        {
            using (var ser = new ECRServiceClient())
            {
                CtServiceResultOfArrayOfCtEmployeeOUJwPUXg result = ser.CtInsertEmployee(OperatorRow);
                if (result.Status == 1)
                {
                    this.Employees = result.Data;
                }
                return new FuncResult<CtEmployee>
                {
                    Message = result.Message,
                    Status = result.Status
                };
            }
        }

        public FuncResult UpdateEmployee()
        {
            using (var ser = new ECRServiceClient())
            {
                CtServiceResultOfArrayOfCtEmployeeOUJwPUXg result = ser.CtUpdateEmployee(OperatorRow);
                if (result.Status == 1)
                {
                    this.Employees = result.Data;
                }
                return new FuncResult<CtEmployee>
                {
                    Message = result.Message,
                    Status = result.Status
                };
            }
        }
    }
}
